from fastapi import APIRouter
from pydantic import BaseModel
from typing import Union
response_mode = APIRouter()



class UserIn(BaseModel):
    username: str
    password: str
    email: str
    full_name: str | None = None

class UserOut(BaseModel):
    username: str
    email: str
    full_name: str | None = None

class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: list[str] = []

items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The best product", "price": 62.3},
    "baz": {"name": "Baz", "description": "There are many variations of passages of ", "price": 56.0, "tax": 10.0, "tags": ["tag1","tag2"]},
}


# 1. response_model 指定返回类型
@response_mode.post("/user",response_model=UserOut)
def create_user(user: UserIn):
    return user


# 2. response_model_exclude_unset 指定返回类型
@response_mode.post("/items/{item_id}",response_model=Item,response_model_exclude_unset=True)
def read_item(item_id: str):
    return items[item_id]

# 3. response_model_include 指定返回类型
@response_mode.post("/item/{item_id}",response_model=Item,response_model_include={"name","price"})
def read_item(item_id: str):
    return items[item_id]

# 4. response_model_exclude 指定返回类型
@response_mode.post("/ite/{item_id}",response_model=Item,response_model_exclude={"name"})
def read_item(item_id: str):
    return items[item_id]


